
import prompt from '@system.prompt';
import storage from '@system.storage';

const ActionParam = {
    ABILITY_TYPE_EXTERNAL : 0,
    ABILITY_TYPE_INTERNAL : 1,
    // syncOption(Optional, default sync): 0-Sync; 1-Async
    ACTION_SYNC : 0,
    ACTION_ASYNC : 1,
}

function newActionBuilder() {
    var actionBuilder = {
        action : {},
        setBundleName : function(bundleName) {
            this.action.bundleName = bundleName;
            return this;
        },
        setAbilityName : function(abilityName) {
            this.action.abilityName = abilityName;
            return this;
        },
        setMessageCode : function(messageCode) {
            this.action.messageCode = messageCode;
            return this;
        },
        setAbilityType : function(abilityType) {
            this.action.abilityType = abilityType;
            return this;
        },
        setSyncOption : function(syncOption) {
            this.action.syncOption = syncOption;
            return this;
        },
        setData : function(data) {
            this.action.data = data;
            return this;
        },
        getAction() {
            return this.action;
        }
    }
    return actionBuilder;
}

function showToast(message) {
    var messageOb = {
        "message" : message,
        "duration" : 1500
    }
    prompt.showToast(messageOb);
}

const Action = {
    FLOWTO : 1001,
    FULLSCREEN : 1002,
    EXITFULLSCREEN : 1003
}

export default {
    data: {
        input_value: "",
        video_list: null
    },
    onInit() {
        if (this.$app.$def.loading_promise != null) {
            this.$app.$def.loading_promise.then(this.doInitOnce).catch(this.loadingErr);
        } else {
            this.doInit();
        }
    },
    doInitOnce() {
        if (this.video_list == null) {
            this.doInit();
        }
    },
    doInit() {
        this.video_list = this.$app.$def.global.video_list;
        this.input_value = this.$app.$def.global.input_url;
    },
    onClickItem(e) {
        var index = parseInt(e);
        this.input_value = this.video_list[index];
        showToast(e);
    },
    onLongPressItem(e) {
        var video_list = this.video_list;
        prompt.showDialog({
            title: 'Confirm',
            message: "Do you want to delete " + e,
            buttons: [
                {
                    text: 'confirm',
                    color: '#0078FD'
                },
                {
                    text : 'cancel',
                    color: '#0C2F55'
                }
            ],
            success: function(data) {
                if (data.index == 0) {
                    console.log(e);
                    var index = parseInt(e);
                    video_list.splice(index, 1);
                    showToast("delete " + e);
                } else {
                    showToast("cancel");
                }
            },
            cancel: function() {
                showToast("cancel");
            },
            data : e
        });
    },
    loadingErr(e) {
        console.log(e);
        showToast("Fail to load data");
    },
    onInputUrlChange(e) {
        this.input_value = e.value;
        this.$app.$def.global.input_url = e.value;
    },
    onAddVideoClick() {
        this.video_list.push({
            "title" : this.video_list.length,
            "url" : this.input_value
        })
    }
}
